//
//  Problem1662.swift
//  TestProject
//
//  Created by 武侠 on 2021/3/15.
//  Copyright © 2021 zhulong. All rights reserved.
//

import UIKit

/*
 1662. 检查两个字符串数组是否相等 【字符判断】
 给你两个字符串数组 word1 和 word2 。如果两个数组表示的字符串相同，返回 true ；否则，返回 false 。

 数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。

 示例 1：
     输入：word1 = ["ab", "c"], word2 = ["a", "bc"]
     输出：true
     解释：
     word1 表示的字符串为 "ab" + "c" -> "abc"
     word2 表示的字符串为 "a" + "bc" -> "abc"
     两个字符串相同，返回 true
 示例 2：
     输入：word1 = ["a", "cb"], word2 = ["ab", "c"]
     输出：false
 示例 3：
     输入：word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
     输出：true
 提示：

     1 <= word1.length, word2.length <= 103
     1 <= word1[i].length, word2[i].length <= 103
     1 <= sum(word1[i].length), sum(word2[i].length) <= 103
     word1[i] 和 word2[i] 由小写字母组成
 */
@objcMembers class Problem1662: NSObject {
    func solution() {
        print(arrayStringsAreEqual(["ab", "c"], ["a", "bc"]))
        print(arrayStringsAreEqual(["a", "b", "cb"], ["ab", "c"]))
        print(arrayStringsAreEqual(["abc", "d", "defg"], ["abcddefg"]))
    }
    
    func arrayStringsAreEqual(_ word1: [String], _ word2: [String]) -> Bool {
        var i = 0
        var ii = -1
        var j = 0
        var jj = -1
        
        while (1 != 0) {
            let c1 = getNextChart(word1, &i, &ii)
            let c2 = getNextChart(word2, &j, &jj)
            
            if c1 == nil && c2 == nil {
                break
            } else if c1 != c2 {
                return false
            }
        }
        
        return true
    }
    
    func getNextChart(_ word: [String], _ i: inout Int, _ ii: inout Int) -> Character? {
        if i >= word.count {
            return nil
        }
        
        var str = word[i]
        ii += 1
        if ii >= str.count {
            i += 1
            if i >= word.count {
                return nil
            }
            ii = 0
            str = word[i]
        }
        return str[str.index(str.startIndex, offsetBy: ii)]
    }
}
